<?php
class power extends core {
	public function __construct() {

		parent::__construct();
	}

	public function index(){
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$where = '';
		if ($keyword) {
			$where = "AND name LIKE '%{$keyword}%' ";
		}
		$sql = "SELECT * FROM jf_admin WHERE 1 {$where} ORDER BY id DESC";
		$this->setpages(array('keyword'=>$keyword));
		$row = $this->ezr->get_results($sql);

		$this->smarty->assign('rs',$row);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->display();
	}
	public function edit(){
		if (IS_POST) {
			$this->save();
		}else{
			//读出权限
			// for( $i=1 ; $i<=66 ; $i++){
			// 	$sql = "INSERT INTO jf_admin_menu( menu_id , admin_id , add_time  ) VALUES 
			// 	( '{$i}' , 7 , '".time()."' )";
			// 	$this->db->query($sql);
			// }
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_admin_menu WHERE admin_id = '{$id}'";
			$menu = $this->db->get_results($sql);
			######//读出菜单
			$p = $this->menu($id);
			// print_r($pa);exit;
			######

			$sql = "SELECT * FROM jf_admin WHERE id='{$id}'";
			$row = $this->db->get_row($sql);

			$index  = $this->search($id , "index");
			$add    = $this->search($id , "add");
			$edit   = $this->search($id , "edit");
			$delete = $this->search($id , "delete");
			// var_dump($index);exit;
			//其它
			$return_goods     = $this->other_search($id , "return_goods");
			$return_edit     = $this->other_search($id , "return_edit");
			$order_index     = $this->other_search($id , "order_index");
			$print_order     = $this->other_search($id , "print_order");
			$grant           = $this->other_search($id , "grant");
			$manual_recharge = $this->other_search($id , "manual_recharge");
			$history         = $this->other_search($id , "history");
			$order           = $this->other_search($id , "order");
			$member          = $this->other_search($id , "member");
			$sales           = $this->other_search($id , "sales");

			$this->smarty->assign('row', $row);
			$this->smarty->assign('menu', $menu);
			$this->smarty->assign('pa', $p);
			
			$this->smarty->assign('index', $index);
			$this->smarty->assign('add', $add);
			$this->smarty->assign('edit', $edit);
			$this->smarty->assign('delete', $delete);
			
			$this->smarty->assign('return_goods', $return_goods);
			$this->smarty->assign('return_edit', $return_edit);
			$this->smarty->assign('order_index', $order_index);
			$this->smarty->assign('print_order', $print_order);
			$this->smarty->assign('manual_recharge', $manual_recharge);
			$this->smarty->assign('grant', $grant);
			$this->smarty->assign('history', $history);
			$this->smarty->assign('order', $order);
			$this->smarty->assign('member', $member);
			$this->smarty->assign('sales', $sales);
			
			$this->display();
		}
		
	}

	public function save(){
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$index = (isset($_POST['index']) && $_POST['index']) ? $_POST['index'] : '';
		$add = (isset($_POST['add']) && $_POST['add']) ? $_POST['add'] : '';
		$edit = (isset($_POST['edit']) && $_POST['edit']) ? $_POST['edit'] : '';
		$delete = (isset($_POST['delete']) && $_POST['delete']) ? $_POST['delete'] : '';
		$other = (isset($_POST['other']) && $_POST['other']) ? $_POST['other'] : '';
		$status = (isset($_POST['status']) && trim($_POST['status'])) ? intval($_POST['status']) : 0;
		$menu = (isset($_POST['menu'])) ? $_POST['menu'] : 0;
		// var_dump($index);
		// var_dump($add);
		// var_dump($edit);
		// var_dump($delete);exit;
		// var_dump($other);exit;
		//菜单权限
		if($menu){
			$sql="DELETE FROM jf_admin_menu WHERE admin_id = '{$id}'";
			$this->db->query($sql);
			foreach ($menu as $key => $val) {
				$sql = "INSERT INTO jf_admin_menu (menu_id , admin_id , add_time) 
						VALUES ('{$val}' , '{$id}' ,'".time()."')";
				$this->db->query($sql);
			}

		}
		//删除原有的
		$sql="DELETE FROM jf_admin_permission WHERE admin_id = '{$id}'";
		$this->db->query($sql);
		//列表权限
		if($index){
			$this->set($index , $id , "index");
		}
		//添加权限
		if($add){
			$this->set($add , $id , "add");
		}
		//编辑权限	
		if($edit){
			$this->set($edit , $id , "edit");
		}
		//删除权限	
		if($delete){
			$this->set($delete , $id , "delete");
		}
		//其它
		if($other){
			foreach ($other as $k => $val) {
				if($val == 'return_goods') {
					$this->other_insert( $id , 'order' , 'return_goods');
				}else if($val == 'return_edit') {
					$this->other_insert( $id , 'order' , 'return_edit');
				}else if($val == 'order_index') {
					$this->other_insert( $id , 'order' , 'order_index');
				}else if($val == 'print_order') {
					$this->other_insert( $id , 'order' , 'print_order');
				}else if($val =='grant' ){
					$this->other_insert( $id , 'coupon' , 'grant');
				}else if($val == 'manual_recharge'){
					$this->other_insert( $id , 'recharge' , 'manual_recharge');
				}else if($val == 'history'){
					$this->other_insert( $id , 'recharge' , 'history');
				}else if($val == 'order'){
					$this->other_insert( $id , 'statistics' , 'order');
				}else if($val == 'member'){
					$this->other_insert( $id , 'statistics' , 'member');
				}else if($val == 'sales'){
					$this->other_insert( $id , 'statistics' , 'sales');
				}else{
					return false;
				}
			}
		}
		$sql = "UPDATE jf_admin SET status = '{$status}' WHERE id ='{$id}'";
		$this->db->query($sql);
		header("Location:?app=power&act=index");
	}

	private function set($arr , $id , $act){
		foreach ($arr as $k => $val) {
			$sql = "INSERT INTO jf_admin_permission( admin_id , app , act  ) VALUES( '{$id}' , '{$val}' , '{$act}' )";
			$this->db->query($sql);
		}
		return true;
	}
	//组装一维数组
	private function make_arr($arr){
		$a =array(); 
		if($arr){
			foreach ($arr as $key => $val) {
				$a[$key] = $val->app;
			}
		}
		return $a;
	}
	//搜索数据
	private function search($id , $act){
		$sql = "SELECT app FROM jf_admin_permission WHERE admin_id='{$id}' AND act ='{$act}'";
		$row = $this->make_arr($this->db->get_results($sql));
		return $row;
	}
	private function other_search($id , $act){
		$sql = "SELECT app FROM jf_admin_permission WHERE admin_id='{$id}' AND act ='{$act}'";
		$row = $this->db->get_row($sql);
		return $row;
	}
	private function other_insert($id , $app , $act){
		$sql = "INSERT INTO jf_admin_permission( admin_id , app , act  ) VALUES( '{$id}' , '{$app}' , '{$act}' )";
		$this->db->query($sql);
		return true;
	}








}
?>